package com.youhong.cuptime.blesdk;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.util.List;

public class RFLampDevice extends Bledevice {

    //This action would be broadcasted after RX and TX characteristic object is got.
    public static final String ACTION_SERVICES_DISCOVERED = "com.lem.babybrush.lem.ble.service.ACTION_SERVICES_DISCOVERED";

    private BluetoothGattCharacteristic TXCharateristic;
    private BluetoothGattCharacteristic RXCharateristic;
    private BluetoothGattCharacteristic CheckCharateristic;
    private BluetoothGattCharacteristic VoltageCharacteristic;
    private BluetoothGattCharacteristic CountCharacteristic;

    public RFLampDevice(Context context, BluetoothDevice device) {
        // TODO Auto-generated constructor stub
        super(context, device.getAddress());
        this.device = device;
    }

    @Override
    protected void discoverCharacteristicsFromService() {
        // ���޸�
        if (bleService == null || device == null) {

            return;
        }
        List<BluetoothGattService> services = bleService
                .getSupportedGattServices();

        if (services == null) {
            return;
        }

        boolean a = false, b = false, c = false, d = false, e = false;

        for (BluetoothGattService service : services) {

            Log.e("Service ID:", "" + service.getUuid().toString());

            for (BluetoothGattCharacteristic characteristic : service
                    .getCharacteristics()) {

                if (service.getUuid().
                        toString().toUpperCase().contains("FEE7")) {

                    if (characteristic.getUuid().toString().toUpperCase().contains("FFE3")) {
                        RXCharateristic = characteristic;
                        this.setCharacteristicNotification(characteristic, true);
                        a = true;
                        Log.e("abc", "a");

                    } else if (characteristic.getUuid().toString().toUpperCase().contains("FEA1")) {

                        CountCharacteristic = characteristic;
                        new Thread() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(300);
                                } catch (InterruptedException e1) {
                                    e1.printStackTrace();
                                }

                                if (RFLampDevice.this != null) {

                                    RFLampDevice.this.setCharacteristicNotification(CountCharacteristic, true);
                                    Log.e("abc", "e");
                                }
                            }
                        }.start();
                        e = true;

                    } else if (characteristic.getUuid().toString().toUpperCase().contains("FFE2")) {

                        TXCharateristic = characteristic;
                        b = true;

                        Log.e("abc", "b");
                    } else if (characteristic.getUuid().toString().toUpperCase().contains("FFE1")) {

                        CheckCharateristic = characteristic;
                        c = true;
                        Log.e("abc", "c");

                    } else if (characteristic.getUuid().toString().toUpperCase().contains("FE24")) {

                        VoltageCharacteristic = characteristic;
                        this.setCharacteristicNotification(characteristic, true);
                        d = true;
                        Log.e("abc", "d");

                    }

                    if (a && b && c && d && e) {

                        context.sendBroadcast(new Intent(RFLampDevice.ACTION_SERVICES_DISCOVERED));
                        return;
                    }
                }

            }
        }
    }

    public byte Crc(byte[] value) {
        byte c = 0;
        for (int i = 0; i < 19; i++) {
            c += value[i];
        }
        return c;
    }

    public int bcd(String s) {

        Integer i = Integer.parseInt(s, 16);
        return i;
    }

    public void writeTx(byte[] values) {


        if (TXCharateristic == null) {
            System.out.println("TXCharateristic is null");
            return;
        }

        TXCharateristic.setValue(values);
        this.writeValue(TXCharateristic);
    }

    public void writeCheck(byte[] values) {

        if (CheckCharateristic == null) {
            System.out.println("CheckCharacteristic is null");
            return;
        }

        CheckCharateristic.setValue(values);
        this.writeValue(CheckCharateristic);
    }

}
